package cn.kgc.crowdfundingadmin.controller;

import cn.kgc.crowdfundingadmin.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadmin.common.ResultEntity;
import cn.kgc.crowdfundingadmin.pojo.Admin;
import cn.kgc.crowdfundingadmin.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-02-23 9:29
 */
@Controller
public class AdminController {


    @Resource
    AdminService adminService;

    //测试 项目是否可以运转  使用了ajax
    @RequestMapping("/admin/get_by_id")
    @ResponseBody
    public Admin getByID(Integer id, Model model){
        Admin admin = adminService.selectByPrimaryKey(id);
        return  admin;
    }

    //登陆成功  管理员
   /* @RequestMapping("/admin/do/login")
    public String doLogin(@RequestParam("loginAcct") String loginAcct,
                          @RequestParam("userPswd") String userPswd,
                          Model model,
                          HttpSession session){
        Admin login = adminService.login(loginAcct, userPswd);
        if(login==null){
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE,CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            //登陆失败  返回登陆页面 并提示 登录账号或密码不正确！请核对后再登录！
            return "admin-login";
        }
        session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN,login);
        //登陆成功 把账号传过去 并调到 admin-main页面
        *//*    return "admin-main";*//*
        *//*跳转改成重定向 刷新没有提示信息*//*
        return "redirect:/admin/to/main/page.html";
    }*/
    //异常处理
    @ExceptionHandler(value=Exception.class)
    public String catchException(Exception exception,Model model) {
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE, exception);
        return "system-error";
    }
        //退出  跳转
    @RequestMapping("/admin/logout")
    public String logout(HttpSession session) {
        //清空
        session.invalidate();
        return "redirect:/index";
    }
    //分页查询用
    @RequestMapping("/admin/query/for/search")
//    @PreAuthorize(value = "hasAnyAuthority('user:get')")
    //    @PreAuthorize(value = "hasAuthority('user:get')")
//    @PreAuthorize(value ="hasRole('超级管理员')")
    @PreAuthorize(value = "hasAnyRole('超级管理员','董事长')") //如果没权限会触发局部异常处理
    public String queryForSearch(
            @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5") Integer pageSize,
            @RequestParam(value="keyword", defaultValue="") String keyword,
            Model model){
        PageInfo<Admin> pageInfo = adminService.queryForKeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO,pageInfo);
        return "admin-page";
    }

    //批量删除
    @ResponseBody
    @RequestMapping("/admin/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdList) {
      /*  adminService.batchRemove(adminIdList);
        return  ResultEntity.successWithoutData();*/
        adminService.batchRemove(adminIdList);

        return ResultEntity.successWithoutData();
    }

    //增加
    @RequestMapping("/admin/save")
    public String saveAdmin(Admin admin) {
        adminService.saveAdmin(admin);
        //增加成功条状到按分页查询的方法 进行查询
        return "redirect:/admin/query/for/search";
    }

    //修改前的查询
    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId,
                             @RequestParam("pageNum") Integer pageNum, Model model) {
        Admin adminById = adminService.getAdminById(adminId);
        model.addAttribute(CrowdFundingConstant.ATTR_ADMIN,adminById);
        //查询完成以后跳转到修改页面
        return "admin-edit";
    }
    //修改页面数据
    @RequestMapping("/admin/update")
    public String updateAdmin(Admin admin,Integer pageNum) {
        adminService.updateAdmin(admin);
        return "redirect:/admin/query/for/search?pageNum="+pageNum;
    }

    @RequestMapping("/403")
    public String noAuth() {
        return "error403";
    }

}
